{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "# GPU\n",
    "\n",
    "查看显卡信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "origin_pos": 1,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sun Nov  7 23:29:29 2021       \n",
      "+-----------------------------------------------------------------------------+\n",
      "| NVIDIA-SMI 460.91.03    Driver Version: 460.91.03    CUDA Version: 11.2     |\n",
      "|-------------------------------+----------------------+----------------------+\n",
      "| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |\n",
      "| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |\n",
      "|                               |                      |               MIG M. |\n",
      "|===============================+======================+======================|\n",
      "|   0  GeForce RTX 3090    Off  | 00000000:65:00.0 Off |                  N/A |\n",
      "| 49%   58C    P0    41W / 350W |      0MiB / 24265MiB |      0%      Default |\n",
      "|                               |                      |                  N/A |\n",
      "+-------------------------------+----------------------+----------------------+\n",
      "                                                                               \n",
      "+-----------------------------------------------------------------------------+\n",
      "| Processes:                                                                  |\n",
      "|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |\n",
      "|        ID   ID                                                   Usage      |\n",
      "|=============================================================================|\n",
      "|  No running processes found                                                 |\n",
      "+-----------------------------------------------------------------------------+\n"
     ]
    }
   ],
   "source": [
    "!nvidia-smi"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    ":begin_tab:`mindspore`\n",
    "在MindSpore中，每个张量都有一个设备（device），\n",
    "我们通常将其称为上下文（context）。\n",
    "默认情况下，所有变量和相关的计算都分配给CPU。\n",
    "有时上下文可能是GPU。\n",
    "当我们跨多个服务器部署作业时，事情会变得更加棘手。\n",
    "通过智能地将数组分配给上下文，\n",
    "我们可以最大限度地减少在设备之间传输数据的时间。\n",
    "例如，当在带有GPU的服务器上训练神经网络时，\n",
    "我们通常希望模型的参数在GPU上。\n",
    "\n",
    "接下来，我们需要确认安装了MindSpore的GPU版本。\n",
    "如果已经安装了MindSpore的CPU版本，我们需要先卸载它。\n",
    "然后根据你的CUDA版本安装相应的PaddlePaddle的GPU版本。\n",
    "例如，假设你安装了CUDA11.1，你可以通过`conda install mindspore-gpu=1.9.0 cudatoolkit=11.1 -c mindspore -c conda-forge`安装支持CUDA11.1的MindSpore版本。\n",
    ":end_tab:"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "设置计算设备\n",
    "\n",
    ":begin_tab:`mindspore`\n",
    "在MindSpore中，CPU和GPU可以用`mindspore.set_context(device_target='CPU')`\n",
    "和`mindspore.set_context(device_target='GPU')`表示,\n",
    "如果没有设置`device_target`则使用MindSpore包对应的后端设备。\n",
    "应该注意的是，`cpu`设备意味着所有物理CPU和内存，\n",
    "这意味着MindSpore的计算将尝试使用所有CPU核心。\n",
    "然而，`gpu`设备只代表一个卡和相应的显存。\n",
    "如果有多个GPU，我们使用`mindspore.set_context(device_id=i)`\n",
    "来表示第$i$块GPU（$i$从0开始）。\n",
    "另外，`device_id`不设置时默认为0。\n",
    ":end_tab:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 8,
    "tab": [
     "pytorch"
    ],
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "MindSpore当前对于context仅在第一次设置时生效，因此本小节中对于多个GPU环境的设置以及变量计算无法实现，请等待后续版本更新支持。"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  },
  "rise": {
   "autolaunch": true,
   "enable_chalkboard": true,
   "overlay": "<div class='my-top-right'><img height=80px src='http://d2l.ai/_static/logo-with-text.png'/></div><div class='my-top-left'></div>",
   "scroll": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}